/*
    This file is part of Raptor.

    Raptor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Raptor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Raptor.  If not, see <http://www.gnu.org/licenses/>.

 */

#include <QtGui>

#include "importWizard.h"
#include "container.h"

importWizard::importWizard(QWidget *parent)
	: QWizard(parent)
{
	setPage(Page_SelectFile, new selectFilePage);

	myVerifyDataPage =  new verifyDataPage();
	setPage(Page_VerifyData, myVerifyDataPage);

	setStartId(Page_SelectFile);

	setOption(HaveHelpButton, false);
	//setPixmap(QWizard::LogoPixmap, QPixmap(":/images/logo.png"));

	setWizardStyle(ModernStyle);

	setWindowTitle(tr("Import Wizard"));

	urlList = QStringList();
}

bool importWizard::validateCurrentPage()
{
	if(currentId() == Page_VerifyData)
	{
		urlList = QStringList();

		for(int i=0; i<myVerifyDataPage->table()->count(); ++i)
			urlList.append(myVerifyDataPage->table()->item(i)->text());
	}

	return true;
}

selectFilePage::selectFilePage(QWidget *parent)
	: QWizardPage(parent)
{
	setTitle(tr("Select File"));
	//setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark.png"));

	topLabel = new QLabel(tr("Please select download container file."));
	topLabel->setWordWrap(true);

	lineEditFileName = new QLineEdit("");
	registerField("fileName*", lineEditFileName);

	pushButtonOpenFile = new QPushButton("Open");

	connect(pushButtonOpenFile, SIGNAL(clicked()), this, SLOT(slot_openFile_clicked()));

	QPointer<QGridLayout> layout = new QGridLayout;

	layout->addWidget(topLabel, 0, 0);
	layout->addWidget(lineEditFileName, 1, 0);
	layout->addWidget(pushButtonOpenFile, 1, 1);

	setLayout(layout);
}

void selectFilePage::slot_openFile_clicked()
{
	QString fileName = QFileDialog::getOpenFileName(this, tr("Open container"), lineEditFileName->text(), tr("RSD Files (*.rsdf)"));
	if(fileName != "") lineEditFileName->setText(fileName);
}

int selectFilePage::nextId() const
{
	return importWizard::Page_VerifyData;
}

verifyDataPage::verifyDataPage(QWidget *parent)
	: QWizardPage(parent)
{
	setTitle(tr("Verify container"));
	//setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark.png"));

	topLabel = new QLabel(tr("Please verify container data."));
	topLabel->setWordWrap(true);

	listWidget = new QListWidget;

	QPointer<QGridLayout> layout = new QGridLayout;
	layout->addWidget(topLabel, 0, 0);
	layout->addWidget(listWidget, 1, 0);

	setLayout(layout);
}

void verifyDataPage::initializePage()
{
	QFile file(field("fileName").toString());

	listWidget->clear();

	if(file.exists())
	{
		container decrypt;
		QStringList fileList = decrypt.decryptRSDF(file);

		for(int i=0; i<fileList.count(); i++)
		{
			//QUrl url(fileList.at(i));
			//int pos = url.toString().lastIndexOf("/") + 1;

			listWidget->addItem(fileList.at(i).trimmed()); //url.toString());
		}
	}
}

int verifyDataPage::nextId() const
{
	return -1;
}
